package iosr11.Service;

import iosr11.data.Attendence;
import iosr11.data.Event;
import iosr11.data.Student;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortResult {

	public static void sortStudents(List<Student> lista){
		Collections.sort(lista, new StudentComparator());
	}
	
	public static void sortAllStudents(List<Student> lista){
		Collections.sort(lista, new AllStudentComparator());
	}
	
	public static void sortEvents(List<Event> lista){
		Collections.sort(lista, new EventComparator());
	}
	
	public static void sortAttendences(List<Attendence> lista){
		Collections.sort(lista, new AttendenceComparator());
	}
	
	/*
	public static void sortAttendences(List<Attendence> lista){
		Collections.sort();
	}*/
}


class AttendenceComparator implements Comparator<Attendence>{
	@Override
	public int compare(Attendence arg0, Attendence arg1) {
		if (arg0==null)return -1;
		else if (arg1==null) return 1;
		else if (arg0.getStudent()==null) return -1;
		else if (arg1.getStudent()==null) return 1;
		else if (arg0.getStudent().getProjectGrp()==null) return -1;
		else if (arg1.getStudent().getProjectGrp()==null) return 1;
		else if (arg0.getStudent().getProjectGrp().getGroupName()==null) return -1;
		else if (arg1.getStudent().getProjectGrp().getGroupName()==null) return 1;
		else {
			return arg0.getStudent().getProjectGrp().getGroupName().compareTo(arg1.getStudent().getProjectGrp().getGroupName());
		}
	}
}

class EventComparator implements Comparator<Event>{

	@Override
	public int compare(Event arg0, Event arg1) {
		if (arg0==null) return -1;
		else if (arg1==null) return 1;
		else if (arg0.getStartDate()==null) return -1;
		else if (arg1.getStartDate()==null) return 1;
		else return arg0.getStartDate().compareTo(arg1.getStartDate());
	}
	
}

class AllStudentComparator implements Comparator<Student>{
	@Override
	public int compare(Student o1, Student o2) {
		if (o1==null){
			return -1;
		}
		else if (o2==null){
			return 1;
		}
		else
			return o1.getName().compareTo(o2.getName());
	}
}


class StudentComparator implements Comparator<Student>{

	@Override
	public int compare(Student o1, Student o2) {
		if (o1.getProjectGrp()!=null){
			if (o2.getProjectGrp()!=null){
				if (o1.getProjectGrp().getGroupName().equals(o2.getProjectGrp().getGroupName())){
					// ustawienie po nazwie
					return o1.getName().compareTo(o2.getName());
				}
				else {
					return o1.getProjectGrp().getGroupName().compareTo(o2.getProjectGrp().getGroupName());
				}
			}
			else {
				return -1;
			}
		}
		else {
			if (o2.getProjectGrp()!=null){
				return 1;
			}
			else {
				return o1.getName().compareTo(o2.getName());
			}
		}
	}
	
}
